# planting_records/models.py
from django.db import models
from django.contrib.auth.models import User
from field_management.models import Field # Import Field model from the other app
from django.urls import reverse

class PlantingRecord(models.Model):
    """记录一次种植活动"""
    PLANTING_METHODS = [
        ('DIRECT', '直播'),
        ('TRANSPLANT', '移栽'),
    ]

    field = models.ForeignKey(Field, on_delete=models.CASCADE, related_name='planting_records', verbose_name="所属田块")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="记录人") # Redundant? Maybe inherit from Field's owner? Let's keep it for flexibility.
    rice_variety = models.CharField(max_length=100, verbose_name="水稻品种")
    planting_date = models.DateField(verbose_name="种植日期")
    seedling_source = models.CharField(max_length=150, blank=True, null=True, verbose_name="秧苗/种子来源")
    planting_method = models.CharField(max_length=20, choices=PLANTING_METHODS, default='TRANSPLANT', verbose_name="种植方式")
    expected_harvest_date = models.DateField(blank=True, null=True, verbose_name="预计收获日期")
    notes = models.TextField(blank=True, null=True, verbose_name="备注")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return f"{self.field.name} - {self.rice_variety} ({self.planting_date})"

    def get_absolute_url(self):
        return reverse('planting_records:record_detail', kwargs={'pk': self.pk})

    class Meta:
        verbose_name = "种植记录"
        verbose_name_plural = "种植记录"
        ordering = ['-planting_date', '-created_at']